import objKeySort from './objKeySort.js'
import Settings from '@/settings/projectSetting.js'
import md5 from 'spark-md5'
/**
 * @description 接口签名验证
 * @param {object} params 参
 * @param {string} t 时间戳
 * @param {string} n 签名字符串
 */
function signature(params, t = null, n = null) {
  const nonce =
    n ||
    Math.random()
      .toString(32)
      .substr(4)
  const timestamp = t || Math.round(new Date() / 1000)
  const signString = Settings.globAppKey + '|' + objKeySort(params) + '|' + timestamp + '|' + nonce
  const sign = md5.hash(signString).toUpperCase()
  return {
    'E-APP-TIMESTAMP': timestamp,
    'E-APP-NONCE': nonce,
    'E-APP-SIGN': sign
  }
}

export default signature
